<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>SSE Demo</title>
</head>
<body>
<h1>时间流</h1>
<ul id="timeList"></ul>
<h1>股票行情</h1>
<ul id="stockList"></ul>

<script>
    // 1. 时间
    const timeSource = new EventSource('sse/time');
    // 监听自定义事件 time
    timeSource.addEventListener('time', function (e) {
        const li = document.createElement('li');
        li.textContent = e.data;
        document.getElementById('timeList').appendChild(li);
    });

    // 2. 股票
    const stockSource = new EventSource('sse/stocks/000001');
    stockSource.addEventListener('price', e => {
        const li = document.createElement('li');
        li.textContent = JSON.parse(e.data).code + ' : ' + JSON.parse(e.data).price.toFixed(2);
        document.getElementById('stockList').appendChild(li);
    });
</script>
</body>
</html>